package com.mhy.netsafe.utils.manager;

import android.annotation.SuppressLint;
import android.util.Log;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * author    : mahongyin
 * e-mail    : mhy.work@qq.com
 * date      : 2019/3/20 10:23
 * introduce :
 */

public class DateManager {

    /**
     * 将毫秒值转化成时间字符串
     *
     * @param millisecond
     * @return
     */
    @SuppressWarnings("JavaDoc")
    public static String millisecondConvertedToDate(String millisecond) {
        long millisecondLong = Long.parseLong(millisecond);
        Date date = new Date(millisecondLong);
        @SuppressLint("SimpleDateFormat")
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }

    /**
     * 将时间字符串转化成毫秒数
     *
     * @param dateStr
     * @return
     */
    @SuppressWarnings("JavaDoc")
    public static long dateStrConvertedToMillisecond(String dateStr) throws ParseException {
        //24小时制
        @SuppressLint("SimpleDateFormat")
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        long time = simpleDateFormat.parse(dateStr).getTime();
        return time;
    }

    /**
     * 将日期转化成时间
     *
     * @param date
     * @return
     */
    public static String getTime(Date date) {//可根据需要自行截取数据显示
        Log.d("getTime()", "choice date millis: " + date.getTime());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    public static String getTime(Date date, String format) {//可根据需要自行截取数据显示
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    /**
     * 将毫秒值转化成年月日格式如（2019-01-05）
     *
     * @param millisecond
     * @return
     */
    @SuppressWarnings("JavaDoc")
    public static String millisecondConvertedToDate(Long millisecond) {
        long millisecondLong = millisecond;
        Date date = new Date(millisecondLong);
        @SuppressLint("SimpleDateFormat")
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }

    /**
     * 将日期转化成年月日格式如（2019/01/05）
     *
     * @param millisecond
     * @return
     */
    @SuppressWarnings("JavaDoc")
    public static String millisecondSwitchToDate(Long millisecond) {
        long millisecondLong = millisecond;
        Date date = new Date(millisecondLong);
        @SuppressLint("SimpleDateFormat")
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = sdf.format(date);
        String dateArr[] = dateStr.split("-");
        dateStr = dateArr[0] + "/" + dateArr[1] + "/" + dateArr[2];
        return dateStr;
    }

    /**
     * 将日期转化成年月日格式如（2019/01/05）
     *
     * @param dateStr
     * @return
     */
    @SuppressWarnings("JavaDoc")
    public static String millisecondSwitchToDate(String dateStr) {
        String dateArr[] = dateStr.split("-");
        String dateStr2 = dateArr[0] + "/" + dateArr[1] + "/" + dateArr[2];
        return dateStr2;
    }

    /**
     * 根据时间戳来判断当前的时间是几天前,几分钟,刚刚
     *
     * @param longTime
     * @return
     */
    public static String getTimeStateNew(String longTime) {
        String long_by_13 = "1000000000000";
        String long_by_10 = "1000000000";
        if (Long.valueOf(longTime) / Long.valueOf(long_by_13) < 1) {
            if (Long.valueOf(longTime) / Long.valueOf(long_by_10) >= 1) {
                longTime = longTime + "000";
            }
        }
        Timestamp time = new Timestamp(Long.valueOf(longTime));
        Timestamp now = new Timestamp(System.currentTimeMillis());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//    System.out.println("传递过来的时间:"+format.format(time));
//    System.out.println("现在的时间:"+format.format(now));
        long day_conver = 1000 * 60 * 60 * 24;
        long hour_conver = 1000 * 60 * 60;
        long min_conver = 1000 * 60;
        long time_conver = now.getTime() - time.getTime();
        long temp_conver;
//    System.out.println("天数:"+time_conver/day_conver);
        if ((time_conver / day_conver) < 3) {
            temp_conver = time_conver / day_conver;
            if (temp_conver <= 2 && temp_conver >= 1) {
                return temp_conver + "天前";
            } else {
                temp_conver = (time_conver / hour_conver);
                if (temp_conver >= 1) {
                    return temp_conver + "小时前";
                } else {
                    temp_conver = (time_conver / min_conver);
                    if (temp_conver >= 1) {
                        return temp_conver + "分钟前";
                    } else {
                        return "刚刚";
                    }
                }
            }
        } else {
            return format.format(time);
        }
    }
}
